package com.hunttown.mes.manage.controller._basic;

import com.hunttown.mes.common.OperateCode;
import com.hunttown.mes.manage.controller.common.AdminBaseClass;
import com.hunttown.mes.manage.service.TransportOperateLogManageService;
import com.hunttown.mes.rpc.domain.AnalysisConfigParamDTO;
import com.hunttown.mes.manage.service.AnalysisConfigParamManageService;
import com.hunttown.mes.manage.service.AnalysisManageManageService;
import com.hunttown.mes.rpc.domain.AnalysisManageDTO;
import com.hunttown.mes.rpc.domain.query.AnalysisConfigParamDTOQuery;
import com.hunttown.common.domain.Page;
import com.hunttown.mes.common.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

import static com.hunttown.mes.common.utils.PageUtils.PagePackage;

/**
 * @title: 站内参数
 * @author: wangjunfu
 * @date: 2018-11-27 16:34:25
 * @description:
 */
@Controller
@RequestMapping(value = "/basic/configparam")
public class ConfigParamController {

    private final static Logger logger = LoggerFactory.getLogger(ConfigParamController.class);

    private final AnalysisConfigParamManageService configParamService;
    private final AnalysisManageManageService manageService;
    private final TransportOperateLogManageService logService;

    @Autowired
    public ConfigParamController(AnalysisConfigParamManageService configParamService, AnalysisManageManageService manageService, TransportOperateLogManageService logService) {
        this.configParamService = configParamService;
        this.manageService = manageService;
        this.logService = logService;
    }

    // 首页
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model, AnalysisConfigParamDTOQuery objDTOQuery) {

        objDTOQuery.setOrderBy("order by id desc");
        Page<AnalysisConfigParamDTO> dataPage = configParamService.getForPage(objDTOQuery);
        model.addAttribute("page", dataPage);
        model.addAttribute("PagePackage", PagePackage(dataPage.getPn(), dataPage.getStart(), dataPage.getPageSize(), dataPage.getTotalPages(), dataPage.getTotalItems()));

        model.addAttribute("dtoQuery", objDTOQuery);

        return "basic/configparam/index";
    }

    // 编辑
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Model model, Integer id) {
        id = id == null ? 0 : id;

        AnalysisConfigParamDTO obj = new AnalysisConfigParamDTO();
        obj.setId(id);

        if (id > 0) {
            obj = configParamService.getById(id);
        }

        model.addAttribute("Obj", obj);

        return "basic/configparam/edit";
    }

    // 参数集中配置
    @RequestMapping(value = "/configSetting", method = RequestMethod.GET)
    public String configSetting(Model model) {

        //是否开启匿名注册小程序
        AnalysisConfigParamDTO anonymousregisterwxappObj = configParamService.getByParamKey("anonymous-register-wxapp");
        if (anonymousregisterwxappObj == null) {
            anonymousregisterwxappObj = new AnalysisConfigParamDTO();
            anonymousregisterwxappObj.setParamKey("anonymous-register-wxapp");
            anonymousregisterwxappObj.setParamInfo("是否开启匿名注册小程序");
        }
        model.addAttribute("anonymousregisterwxappObj", anonymousregisterwxappObj);

        //数据报表同比环比大于30%即判断为异常
        AnalysisConfigParamDTO billexceptionrateObj = configParamService.getByParamKey("bill-exception-rate");
        if (billexceptionrateObj == null) {
            billexceptionrateObj = new AnalysisConfigParamDTO();
            billexceptionrateObj.setParamKey("bill-exception-rate");
            billexceptionrateObj.setParamInfo("数据报表同比环比大于30%即判断为异常");
        }
        model.addAttribute("billexceptionrateObj", billexceptionrateObj);

        //站内所有缓存过期时间，默认24小时（1440分钟）
        AnalysisConfigParamDTO cacheexpiretimeObj = configParamService.getByParamKey("cache-expire-time");
        if (cacheexpiretimeObj == null) {
            cacheexpiretimeObj = new AnalysisConfigParamDTO();
            cacheexpiretimeObj.setParamKey("cache-expire-time");
            cacheexpiretimeObj.setParamInfo("站内所有缓存过期时间，默认24小时（1440分钟）");
        }
        model.addAttribute("cacheexpiretimeObj", cacheexpiretimeObj);

        //设备离线判断时间（单位：分钟），如果没有心跳功能，则以轮询时间为准。
        //AnalysisConfigParamDTO equipoutlineObj = configParamService.getByParamKey("equip-outline");
        //if (equipoutlineObj == null) {
        //    equipoutlineObj = new AnalysisConfigParamDTO();
        //    equipoutlineObj.setParamKey("equip-outline");
        //    equipoutlineObj.setParamInfo("设备离线判断时间（单位：分钟），如果没有心跳功能，则以轮询时间为准。");
        //}
        //model.addAttribute("equipoutlineObj", equipoutlineObj);

        //电表开合闸判断时间（单位：分钟），以轮询时间为准。
        //AnalysisConfigParamDTO equipoutpowerObj = configParamService.getByParamKey("equip-outpower");
        //if (equipoutpowerObj == null) {
        //    equipoutpowerObj = new AnalysisConfigParamDTO();
        //    equipoutpowerObj.setParamKey("equip-outpower");
        //    equipoutpowerObj.setParamInfo("电表开合闸判断时间（单位：分钟），以轮询时间为准。");
        //}
        //model.addAttribute("equipoutpowerObj", equipoutpowerObj);

        //首页门户地址
        AnalysisConfigParamDTO indexdashboardurlObj = configParamService.getByParamKey("index-dashboard-url");
        if (indexdashboardurlObj == null) {
            indexdashboardurlObj = new AnalysisConfigParamDTO();
            indexdashboardurlObj.setParamKey("index-dashboard-url");
            indexdashboardurlObj.setParamInfo("首页门户地址");
        }
        model.addAttribute("indexdashboardurlObj", indexdashboardurlObj);

        //登录后显示的主页
        AnalysisConfigParamDTO indexmainObj = configParamService.getByParamKey("index-main");
        if (indexmainObj == null) {
            indexmainObj = new AnalysisConfigParamDTO();
            indexmainObj.setParamKey("index-main");
            indexmainObj.setParamInfo("登录后显示的主页");
        }
        model.addAttribute("indexmainObj", indexmainObj);

        //一次登录保存多长的存活时间。单位：小时
        AnalysisConfigParamDTO loginkeepliveObj = configParamService.getByParamKey("login-keep-live");
        if (loginkeepliveObj == null) {
            loginkeepliveObj = new AnalysisConfigParamDTO();
            loginkeepliveObj.setParamKey("login-keep-live");
            loginkeepliveObj.setParamInfo("一次登录保存多长的存活时间。单位：小时");
        }
        model.addAttribute("loginkeepliveObj", loginkeepliveObj);

        //采购、报销超过300元需要经理审批。（已配置到流程中，不需要此操作）
        //AnalysisConfigParamDTO manageapplyexpenseObj = configParamService.getByParamKey("manage-apply-expense");
        //if (manageapplyexpenseObj == null) {
        //    manageapplyexpenseObj = new AnalysisConfigParamDTO();
        //    manageapplyexpenseObj.setParamKey("manage-apply-expense");
        //    manageapplyexpenseObj.setParamInfo("采购、报销超过300元需要经理审批。");
        //}
        //model.addAttribute("manageapplyexpenseObj", manageapplyexpenseObj);

        //员工请假超过3天需要经理审批。（已配置到流程中，不需要此操作）
        //AnalysisConfigParamDTO manageapplyholidayObj = configParamService.getByParamKey("manage-apply-holiday");
        //if (manageapplyholidayObj == null) {
        //    manageapplyholidayObj = new AnalysisConfigParamDTO();
        //    manageapplyholidayObj.setParamKey("manage-apply-holiday");
        //    manageapplyholidayObj.setParamInfo("员工请假超过3天需要经理审批。");
        //}
        //model.addAttribute("manageapplyholidayObj", manageapplyholidayObj);

        //管理员验证手机号
        AnalysisConfigParamDTO managesecondmobileObj = configParamService.getByParamKey("manage-second-mobile");
        if (managesecondmobileObj == null) {
            managesecondmobileObj = new AnalysisConfigParamDTO();
            managesecondmobileObj.setParamKey("manage-second-mobile");
            managesecondmobileObj.setParamInfo("管理员验证手机号");
        }
        model.addAttribute("managesecondmobileObj", managesecondmobileObj);

        //曼顿空开项目编号
        //AnalysisConfigParamDTO mantunprocodeObj = configParamService.getByParamKey("mantun-pro-code");
        //if (mantunprocodeObj == null) {
        //    mantunprocodeObj = new AnalysisConfigParamDTO();
        //    mantunprocodeObj.setParamKey("mantun-pro-code");
        //    mantunprocodeObj.setParamInfo("曼顿空开项目编号");
        //}
        //model.addAttribute("mantunprocodeObj", mantunprocodeObj);

        //打开左侧菜单，优先级低于部门配置。
        AnalysisConfigParamDTO menuopenObj = configParamService.getByParamKey("menu-open");
        if (menuopenObj == null) {
            menuopenObj = new AnalysisConfigParamDTO();
            menuopenObj.setParamKey("menu-open");
            menuopenObj.setParamInfo("打开左侧菜单，优先级低于部门配置。");
        }
        model.addAttribute("menuopenObj", menuopenObj);

        //索引操作密码
        AnalysisConfigParamDTO operateindextokenObj = configParamService.getByParamKey("operate-index-token");
        if (operateindextokenObj == null) {
            operateindextokenObj = new AnalysisConfigParamDTO();
            operateindextokenObj.setParamKey("operate-index-token");
            operateindextokenObj.setParamInfo("索引操作密码");
        }
        model.addAttribute("operateindextokenObj", operateindextokenObj);

        //温度传感器超过一定时间没有上报则不再执行分合闸操作。
        //AnalysisConfigParamDTO temperaturelimitminutesObj = configParamService.getByParamKey("temperature-limit-minutes");
        //if (temperaturelimitminutesObj == null) {
        //    temperaturelimitminutesObj = new AnalysisConfigParamDTO();
        //    temperaturelimitminutesObj.setParamKey("temperature-limit-minutes");
        //    temperaturelimitminutesObj.setParamInfo("温度传感器超过一定时间没有上报则不再执行分合闸操作。");
        //}
        //model.addAttribute("temperaturelimitminutesObj", temperaturelimitminutesObj);

        //小程序扫码图片地址
        AnalysisConfigParamDTO xcxurlObj = configParamService.getByParamKey("xcx-url");
        if (xcxurlObj == null) {
            xcxurlObj = new AnalysisConfigParamDTO();
            xcxurlObj.setParamKey("xcx-url");
            xcxurlObj.setParamInfo("小程序扫码图片地址");
        }
        model.addAttribute("xcxurlObj", xcxurlObj);

        //菜单通用分类ID配置（改用英文名称，不需要此操作）
        //AnalysisConfigParamDTO rightmenucateidsObj = configParamService.getByParamKey("rightmenu-cateids");
        //if (rightmenucateidsObj == null) {
        //    rightmenucateidsObj = new AnalysisConfigParamDTO();
        //    rightmenucateidsObj.setParamKey("rightmenu-cateids");
        //    rightmenucateidsObj.setParamInfo("菜单通用分类ID配置");
        //}
        //model.addAttribute("rightmenucateidsObj", rightmenucateidsObj);

        //短信提供商：1腾讯云 2专用
        AnalysisConfigParamDTO smssupplierObj = configParamService.getByParamKey("sms-supplier");
        if (smssupplierObj == null) {
            smssupplierObj = new AnalysisConfigParamDTO();
            smssupplierObj.setParamKey("sms-supplier");
            smssupplierObj.setParamInfo("短信提供商：1腾讯云 2专用");
        }
        model.addAttribute("smssupplierObj", smssupplierObj);

        //腾讯云短信：APPID
        AnalysisConfigParamDTO smstencentappidObj = configParamService.getByParamKey("sms-tencent-appid");
        if (smstencentappidObj == null) {
            smstencentappidObj = new AnalysisConfigParamDTO();
            smstencentappidObj.setParamKey("sms-tencent-appid");
            smstencentappidObj.setParamInfo("腾讯云短信APPID");
        }
        model.addAttribute("smstencentappidObj", smstencentappidObj);

        //腾讯云短信：密钥
        AnalysisConfigParamDTO smstencentappkeyObj = configParamService.getByParamKey("sms-tencent-appkey");
        if (smstencentappkeyObj == null) {
            smstencentappkeyObj = new AnalysisConfigParamDTO();
            smstencentappkeyObj.setParamKey("sms-tencent-appkey");
            smstencentappkeyObj.setParamInfo("腾讯云短信APPKEY");
        }
        model.addAttribute("smstencentappkeyObj", smstencentappkeyObj);

        //微信公众号：APPID
        AnalysisConfigParamDTO wxgzhappidObj = configParamService.getByParamKey("wx-gzh-appid");
        if (wxgzhappidObj == null) {
            wxgzhappidObj = new AnalysisConfigParamDTO();
            wxgzhappidObj.setParamKey("wx-gzh-appid");
            wxgzhappidObj.setParamInfo("微信公众号APPID");
        }
        model.addAttribute("wxgzhappidObj", wxgzhappidObj);

        //微信公众号：密钥
        AnalysisConfigParamDTO wxgzhappsecretObj = configParamService.getByParamKey("wx-gzh-appsecret");
        if (wxgzhappsecretObj == null) {
            wxgzhappsecretObj = new AnalysisConfigParamDTO();
            wxgzhappsecretObj.setParamKey("wx-gzh-appsecret");
            wxgzhappsecretObj.setParamInfo("微信公众号APPSecret");
        }
        model.addAttribute("wxgzhappsecretObj", wxgzhappsecretObj);

        //文件上传提供商：0本地存储 1腾讯云 2阿里云
        AnalysisConfigParamDTO cosserviceObj = configParamService.getByParamKey("cosservice-supplier");
        if (cosserviceObj == null) {
            cosserviceObj = new AnalysisConfigParamDTO();
            cosserviceObj.setParamKey("cosservice-supplier");
            cosserviceObj.setParamInfo("文件上传提供商：0本地存储 1腾讯云 2阿里云");
        }
        model.addAttribute("cosserviceObj", cosserviceObj);

        //本地本存储文件夹
        AnalysisConfigParamDTO localCosPathObj = configParamService.getByParamKey("local-cos-path");
        if (localCosPathObj == null) {
            localCosPathObj = new AnalysisConfigParamDTO();
            localCosPathObj.setParamKey("local-cos-path");
            localCosPathObj.setParamInfo("本地本存储文件夹");
        }
        model.addAttribute("localCosPathObj", localCosPathObj);

        //可上传文件及限制大小
        AnalysisConfigParamDTO uploadFileTypeObj = configParamService.getByParamKey("upload-file-type");
        if (uploadFileTypeObj == null) {
            uploadFileTypeObj = new AnalysisConfigParamDTO();
            uploadFileTypeObj.setParamKey("upload-file-type");
            uploadFileTypeObj.setParamInfo("可上传文件及限制大小");
        }
        model.addAttribute("uploadFileTypeObj", uploadFileTypeObj);

        //腾讯云COS配置：SecretID
        AnalysisConfigParamDTO costencentsecretidObj = configParamService.getByParamKey("cos-tencent-secretid");
        if (costencentsecretidObj == null) {
            costencentsecretidObj = new AnalysisConfigParamDTO();
            costencentsecretidObj.setParamKey("cos-tencent-secretid");
            costencentsecretidObj.setParamInfo("腾讯云COS存储SecretID");
        }
        model.addAttribute("costencentsecretidObj", costencentsecretidObj);

        //腾讯云COS配置：SecretKey
        AnalysisConfigParamDTO costencentsecretkeyObj = configParamService.getByParamKey("cos-tencent-secretkey");
        if (costencentsecretkeyObj == null) {
            costencentsecretkeyObj = new AnalysisConfigParamDTO();
            costencentsecretkeyObj.setParamKey("cos-tencent-secretkey");
            costencentsecretkeyObj.setParamInfo("腾讯云COS存储SecretKey");
        }
        model.addAttribute("costencentsecretkeyObj", costencentsecretkeyObj);

        //腾讯云COS配置：存储桶
        AnalysisConfigParamDTO costencentbucketObj = configParamService.getByParamKey("cos-tencent-bucket");
        if (costencentbucketObj == null) {
            costencentbucketObj = new AnalysisConfigParamDTO();
            costencentbucketObj.setParamKey("cos-tencent-bucket");
            costencentbucketObj.setParamInfo("腾讯云COS存储桶Bucket");
        }
        model.addAttribute("costencentbucketObj", costencentbucketObj);

        //腾讯云COS配置：存储地区
        AnalysisConfigParamDTO costencentregionObj = configParamService.getByParamKey("cos-tencent-region");
        if (costencentregionObj == null) {
            costencentregionObj = new AnalysisConfigParamDTO();
            costencentregionObj.setParamKey("cos-tencent-region");
            costencentregionObj.setParamInfo("腾讯云COS存储地区region");
        }
        model.addAttribute("costencentregionObj", costencentregionObj);

        //充值手续费率
        AnalysisConfigParamDTO serviceChargeRateObj = configParamService.getByParamKey("service-charge-rate");
        if (serviceChargeRateObj == null) {
            serviceChargeRateObj = new AnalysisConfigParamDTO();
            serviceChargeRateObj.setParamKey("service-charge-rate");
            serviceChargeRateObj.setParamInfo("充值手续费：如0.6%请输入0.006；0表示不收手续费");
        }
        model.addAttribute("serviceChargeRateObj", serviceChargeRateObj);

        //员工ID：负责单位类别
        AnalysisConfigParamDTO manageDiaoduRangeObj = configParamService.getByParamKey("manager-diaodu-range");
        if (manageDiaoduRangeObj == null) {
            manageDiaoduRangeObj = new AnalysisConfigParamDTO();
            manageDiaoduRangeObj.setParamKey("manager-diaodu-range");
            manageDiaoduRangeObj.setParamInfo("员工ID：负责单位类别");
        }
        model.addAttribute("manageDiaoduRangeObj", manageDiaoduRangeObj);

        //调度部门ID：客户订车成功后下发通知消息
        AnalysisConfigParamDTO messageDiaoduIdObj = configParamService.getByParamKey("department-diaodu-id");
        if (messageDiaoduIdObj == null) {
            messageDiaoduIdObj = new AnalysisConfigParamDTO();
            messageDiaoduIdObj.setParamKey("department-diaodu-id");
            messageDiaoduIdObj.setParamInfo("调度部门ID：客户订车成功后下发通知消息");
        }
        model.addAttribute("messageDiaoduIdObj", messageDiaoduIdObj);

        //主数据库
        AnalysisConfigParamDTO messageDatabaseObj = configParamService.getByParamKey("main-database-name");
        if (messageDatabaseObj == null) {
            messageDatabaseObj = new AnalysisConfigParamDTO();
            messageDatabaseObj.setParamKey("main-database-name");
            messageDatabaseObj.setParamInfo("配置备份数据库");
        }
        model.addAttribute("messageDatabaseObj", messageDatabaseObj);

        //节假日
        AnalysisConfigParamDTO holidayDaysObj = configParamService.getByParamKey("holiday-days");
        if (holidayDaysObj == null) {
            holidayDaysObj = new AnalysisConfigParamDTO();
            holidayDaysObj.setParamKey("holiday-days");
            holidayDaysObj.setParamInfo("节假日日期设置，每年一次");
            holidayDaysObj.setRemark("格式：20220101,20220102,20220103......");
        }
        model.addAttribute("holidayDaysObj", holidayDaysObj);

        //对外联系电话
        AnalysisConfigParamDTO linkPhoneObj = configParamService.getByParamKey("link-phone");
        if (linkPhoneObj == null) {
            linkPhoneObj = new AnalysisConfigParamDTO();
            linkPhoneObj.setParamKey("link-phone");
            linkPhoneObj.setParamInfo("公司对外联系电话");
        }
        model.addAttribute("linkPhoneObj", linkPhoneObj);

        return "basic/configparam/config-setting";
    }

    @RequestMapping(value = "/saveConfigSetting", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveConfigSetting(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();

        String[] param_key = request.getParameterValues("param_key");
        String[] param_value = request.getParameterValues("param_value");
        String[] param_info = request.getParameterValues("param_info");
        String[] remark = request.getParameterValues("remark");

        if (param_key == null) {
            map.put("returnMessage", "未获取到数据！");
            return map;
        }

        String pin = AdminBaseClass.getPin(request);
        AnalysisManageDTO curr_obj = manageService.getFromCachePin(pin);

        for (int i = 0; i < param_key.length; i++) {
            String key = param_key[i];
            String value = param_value[i];
            String info = param_info[i];
            String mark = remark[i];

            //先从库中获取参数数据，如果有则更新，没有则插入
            AnalysisConfigParamDTO obj = configParamService.getByParamKey(key);

            if (obj == null) {
                obj = new AnalysisConfigParamDTO();
                obj.setParamKey(key);
                obj.setParamType(1);
                obj.setParamValue(value);
                obj.setParamInfo(info);
                obj.setRemark(mark);
                obj.setAdminId(curr_obj.getId());
                obj.setCreateTime(new Date());
                obj.setStopFlag(0);
                obj.setDeleteFlag(0);
                configParamService.insert(obj);

            } else {
                AnalysisConfigParamDTO update = new AnalysisConfigParamDTO();
                update.setId(obj.getId());
                update.setParamKey(key);
                update.setParamValue(value);
                update.setParamInfo(info);
                update.setRemark(mark);
                configParamService.updateInfoById(update);
            }
        }

        map.put("returnMessage", "数据保存完毕！");
        return map;
    }

    // 保存
    @RequestMapping(value = "/saveEdit", method = RequestMethod.POST)
    @ResponseBody
    public String saveEditNotify(HttpServletRequest request, AnalysisConfigParamDTO objDTO) {

        //结果返回，结果至少包含一个操作状态和一条状态信息
        String result = "";
        Map<String, String> map = new HashMap<String, String>();
        try {
            if (objDTO.getStopFlag() == null) {
                objDTO.setStopFlag(0);
            }

            //新增时检验是否唯一
            if (objDTO.getId() == null || objDTO.getId() <= 0) {
                String limitStr = configParamService.getByParamKey(objDTO.getParamKey(), "");
                if (!limitStr.equals("")) {
                    //说明已经有此记录了，不能插入。
                    map = OperateCode.CompositionResult(map, -10, "此配置KEY已存在，请重新输入！");
                    result = JSONUtils.toJSON(map);
                    return result;
                }
            }

            String pin = AdminBaseClass.getPin(request);
            AnalysisManageDTO curr_obj = manageService.getFromCachePin(pin);

            if (objDTO.getId() == null || objDTO.getId() <= 0) {
                //新增
                //设置初始值
                objDTO.setAdminId(curr_obj.getId());
                objDTO.setCreateTime(new Date());
                objDTO.setDeleteFlag(0);

                objDTO = configParamService.insert(objDTO);

                if (objDTO == null || objDTO.getId() <= 0) {
                    //操作失败
                    map = OperateCode.CompositionResult(map, -1);
                    result = JSONUtils.toJSON(map);
                    return result;
                }
            } else {
                //更新
                //将不需要更新的字段设置为NULL
                objDTO.setParamKey(null);

                Boolean isNext = configParamService.updateInfoById(objDTO);
                if (!isNext) {
                    //操作失败
                    map = OperateCode.CompositionResult(map, -1);
                    result = JSONUtils.toJSON(map);
                    return result;
                }
            }

            //刷新缓存中的数据

            //region *** 更新信息完毕记录操作日志 ***
            logService.insertSimple(1, 5, objDTO.getId(), 2,
                    "更新网站参数信息",
                    FastJsonUtils.toJSONString(objDTO),
                    curr_obj.getId(),
                    IpUtils.getClientIp(request),
                    SystemUtils.getBrowserInfo(request));
            //endregion

            //操作成功 结果返回
            map = OperateCode.CompositionResult(map, 1);
            result = JSONUtils.toJSON(map);
            return result;
        } catch (Exception e) {
            //清空map
            map.clear();
            map = OperateCode.CompositionResult(map, -1);
            result = JSONUtils.toJSON(map);
            return result;
        }
    }

    // 批量删除
    @RequestMapping(value = "/deleteInfo", method = RequestMethod.POST)
    @ResponseBody
    public String deleteNotify(HttpServletRequest request, String ids) {

        //结果返回，结果至少包含一个操作状态和一条状态信息
        String result = "";
        Map<String, String> map = new HashMap<String, String>();

        try {
            if (StringUtils.isBlank(ids)) {
                map = OperateCode.CompositionResult(map, -10, "要删除的记录不存在，请更正后再进行操作！");
                result = JSONUtils.toJSON(map);
                return result;
            }

            Boolean isNext = configParamService.deleteForBatch(ids);

            //region *** 更新信息完毕记录操作日志 ***
            String pin = AdminBaseClass.getPin(request);
            AnalysisManageDTO curr_obj = manageService.getFromCachePin(pin);
            logService.insertSimple(1, 5, 0, 4,
                    "删除网站参数信息",
                    "网站参数删除：" + (isNext ? "成功" : "失败") + "：" + ids,
                    curr_obj.getId(),
                    IpUtils.getClientIp(request),
                    SystemUtils.getBrowserInfo(request));
            //endregion

            if (isNext) {
                map = OperateCode.CompositionResult(map, 1);
                result = JSONUtils.toJSON(map);
                return result;
            } else {
                map = OperateCode.CompositionResult(map, 0);
                result = JSONUtils.toJSON(map);
                return result;
            }

        } catch (Exception e) {
            //清空map
            map.clear();
            map = OperateCode.CompositionResult(map, -1);
            result = JSONUtils.toJSON(map);
            return result;
        }
    }

    // 状态改变
    @RequestMapping(value = "/changeFlag", method = RequestMethod.POST)
    @ResponseBody
    public String openOrStopFlag(HttpServletRequest request, Integer id, Integer status) {
        //结果返回，结果至少包含一个操作状态和一条状态信息
        String result = "";
        Map<String, String> map = new HashMap<String, String>();

        try {
            Boolean isNext = configParamService.changeState(id, status);

            //region *** 更新信息完毕记录操作日志 ***
            String pin = AdminBaseClass.getPin(request);
            AnalysisManageDTO curr_obj = manageService.getFromCachePin(pin);
            logService.insertSimple(1, 5, id, 4,
                    "启停用网站参数信息",
                    "网站参数启停用：" + (isNext ? "成功" : "失败") + "：" + id,
                    curr_obj.getId(),
                    IpUtils.getClientIp(request),
                    SystemUtils.getBrowserInfo(request));
            //endregion

            map = OperateCode.CompositionResult(map, isNext ? 1 : 0);
            result = JSONUtils.toJSON(map);
            return result;

        } catch (Exception e) {
            //清空map
            map.clear();
            map = OperateCode.CompositionResult(map, -1);
            result = JSONUtils.toJSON(map);
            return result;
        }
    }

    // 工单限时配置
    @RequestMapping(value = "/alarmSetting", method = RequestMethod.GET)
    public String alarmSetting(Model model) {

        //上级派单
        AnalysisConfigParamDTO superiorLimitObj = configParamService.getByParamKey("gmzj-superior-limit");
        if (superiorLimitObj == null) {
            superiorLimitObj = new AnalysisConfigParamDTO();
            superiorLimitObj.setParamKey("gmzj-superior-limit");
            superiorLimitObj.setParamInfo("上级派单完成限时");
        }
        model.addAttribute("superiorLimitObj", superiorLimitObj);

        //公司派单
        AnalysisConfigParamDTO companyLimitObj = configParamService.getByParamKey("gmzj-company-limit");
        if (companyLimitObj == null) {
            companyLimitObj = new AnalysisConfigParamDTO();
            companyLimitObj.setParamKey("gmzj-company-limit");
            companyLimitObj.setParamInfo("公司派单完成限时");
        }
        model.addAttribute("companyLimitObj", companyLimitObj);

        //部门派单
        AnalysisConfigParamDTO departmentLimitObj = configParamService.getByParamKey("gmzj-department-limit");
        if (departmentLimitObj == null) {
            departmentLimitObj = new AnalysisConfigParamDTO();
            departmentLimitObj.setParamKey("gmzj-department-limit");
            departmentLimitObj.setParamInfo("部门派单完成限时");
        }
        model.addAttribute("departmentLimitObj", departmentLimitObj);

        //台区经理派单
        AnalysisConfigParamDTO personLimitObj = configParamService.getByParamKey("gmzj-person-limit");
        if (personLimitObj == null) {
            personLimitObj = new AnalysisConfigParamDTO();
            personLimitObj.setParamKey("gmzj-person-limit");
            personLimitObj.setParamInfo("台区经理派单完成限时");
        }
        model.addAttribute("personLimitObj", personLimitObj);

        return "customer/alarm-config/alarm-setting";
    }

    // 工单数据采集时间设置
    @RequestMapping(value = "/worktimeSetting", method = RequestMethod.GET)
    public String worktimeSetting(Model model) {

        AnalysisConfigParamDTO worktimeObj = configParamService.getByParamKey("go-work-time");
        if (worktimeObj == null) {
            worktimeObj = new AnalysisConfigParamDTO();
            worktimeObj.setParamKey("go-work-time");
            worktimeObj.setParamInfo("位置采集时间设置");
        }
        model.addAttribute("worktimeObj", worktimeObj);
        return "customer/alarm-config/worktime-setting";
    }
}